import ioa from 'ioa';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import router from 'ioa-router';
import debug from './debug.js';
import networkip from './networkip.js';

const { argv } = ioa;
const { config } = ioa.app();

let port = 8800;

if (argv.port && argv.port[0]) {
   port = argv.port[0];
} else if (argv.p && argv.p[0]) {
   port = argv.p[0];
} else if (config && config.port) {
   port = config.port;
}

const koa = new Koa();

koa.use(bodyParser(config.parser));

if (config.debug === true) {
   koa.use(debug);
}

koa.use(router);

koa.listen(port);

console.log("App running at:");

console.log(` - Local: \x1b[36mhttp://localhost:${port}\x1b[30m`);

const ip = networkip();

console.log(` - Network: \x1b[36mhttp://${ip}:${port}\x1b[30m`);
